﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Com.Core.Helps
{
    public class ValidateCodeHelper
    {
        /// <summary>
        /// 產生圖形驗證碼。
        /// </summary>
        /// <param name="code">傳出驗證碼。</param>
        /// <param name="codeLength">驗證碼字元數。</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="fontSize"></param>
        /// <returns></returns>
        public static byte[] CreateValidateGraphic(out String code, int codeLength, int width, int height, int fontSize)
        {
            String sCode = String.Empty;
            //顏色列表，用於驗證碼、噪線、噪點
            Color[] oColors ={
            Color.Black,
             Color.Red,
             Color.Blue,
             Color.Green,
             Color.Brown,
             Color.DarkBlue
            };
            Color[] bColors ={
                                 Color.White
                            };
            //字體列表，用於驗證碼
            string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
            //驗證碼的字元集，去掉了一些容易混淆的字元
            char[] oCharacter = { '2', '3', '4', '5', '6', '8', '9', '0', '1','a','A','B','b','C','c','d','D','e','E','f','F','g','G','h','H','j','J','k','K','L','M','m','n','N','P','p','q','Q','R','r','s','S','T','t','u','U','v','V','w','W','x','X','y','Y' };
            Random oRnd = new Random();
            int n1;
            //System.Drawing.Point oPoint1 = default(System.Drawing.Point);
            //System.Drawing.Point oPoint2 = default(System.Drawing.Point);

            //生成驗證碼字串
            for (n1 = 0; n1 <= codeLength - 1; n1++)
            {
                sCode += oCharacter[oRnd.Next(oCharacter.Length)];
            }

            var oBmp = new Bitmap(width, height);
            var oGraphics = Graphics.FromImage(oBmp);
            //填充背景色
            var bColor = bColors[oRnd.Next(bColors.Length)];
            oGraphics.Clear(bColor);
            try
            {
                //for (N1 = 0; N1 <= 4; N1++)
                //{
                //    //畫噪線
                //    oPoint1.X = oRnd.Next(width);
                //    oPoint1.Y = oRnd.Next(height);
                //    oPoint2.X = oRnd.Next(width);
                //    oPoint2.Y = oRnd.Next(height);
                //    oColor = oColors[oRnd.Next(oColors.Length)];
                //    oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2);
                //}

                float spaceWith = 0;
                if (codeLength != 0)
                {
                    spaceWith = (width - fontSize * codeLength - 10) / codeLength;
                }

                for (n1 = 0; n1 <= sCode.Length - 1; n1++)
                {
                    //畫驗證碼字串
                    var sFontName = oFontNames[oRnd.Next(oFontNames.Length)];
                    var oFont = new Font(sFontName, fontSize, FontStyle.Italic);
                    var oColor = oColors[oRnd.Next(oColors.Length)];

                    float dotY = (height - oFont.Height) / 2 + 2;
                    var dotX = Convert.ToSingle(n1) * fontSize + (n1 + 1) * spaceWith;

                    oGraphics.DrawString(sCode[n1].ToString(), oFont, new SolidBrush(oColor), dotX, dotY);
                }

                for (int i = 0; i < 30; i++)
                {
                    //畫噪點
                    int x = oRnd.Next(oBmp.Width);
                    int y = oRnd.Next(oBmp.Height);
                    Color clr = oColors[oRnd.Next(oColors.Length)];
                    oBmp.SetPixel(x, y, clr);
                }
                code = sCode;
                //保存图片数据
                MemoryStream stream = new MemoryStream();
                oBmp.Save(stream, ImageFormat.Jpeg);
                //输出图片流
                return stream.ToArray();
            }
            finally
            {
                oGraphics.Dispose();
            }
        }
    }
}
